package com.bright.cwduijie.duijie.service;

import com.bright.cwduijie.duijie.enums.DataApi;
import com.bright.cwduijie.duijie.pojo.bo.ApiDataCollection;
import com.bright.cwduijie.duijie.pojo.dto.CheckResultDto;
import com.bright.cwduijie.duijie.pojo.query.DuijieQuery;
import com.bright.cwduijie.duijie.util.ListUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @Author hxj
 * @Date 2024/7/29 13:08
 * @Description 月度报表类型 目前有资产负债表 债务债权公布表 加上凭证 固定资产处置、折旧
 */

@Component
@Slf4j
public abstract class AbstractMonthlyReportApiService<DTO, VO> extends AbstractApiParentService<DTO> {

    public abstract Function<ApiDataCollection, Map<String, List<DTO>>> getDataFunc();

    public abstract Function<DTO, VO> getConverterFunc();

    protected void checkDataValid(Map<String, CheckResultDto> checkResultDtoMap, ApiDataCollection apiDataCollection) {
        checkService.checkDataBeforeSend(checkResultDtoMap, getDataFunc().apply(apiDataCollection), getDataApi(), getIdFunc());
    }

    @Override
    public void sendData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery, String batchId) throws JsonProcessingException {
        sendYearMonthBusinessData(getDataApi(), getDataFunc().apply(apiDataCollection), duijieQuery.getSettledYearMonths(),
                getConverterFunc(), getIdFunc(), batchId, duijieQuery.getAccSet());
    }

    // 发送财务业务数据 按会计期间的
    protected void sendYearMonthBusinessData(DataApi dataApi, Map<String, List<DTO>> dataMapByYearMonth, List<String> settledYearMonths,
                                                       Function<DTO, VO> converterFunc, Function<DTO, Integer> getIdFunc, String batchId, Integer accSet) throws JsonProcessingException {

        if (dataMapByYearMonth.size() == 0) {
            // 没数据的情况
            sendMapData(dataMapByYearMonth, dataApi, new ArrayList<>(), batchId, accSet);
        }
        // 按照会计期间进行划分
        for (Map.Entry<String, List<DTO>> entry : dataMapByYearMonth.entrySet()) {
//            String yearMonth = entry.getKey();
            List<DTO> dtos = entry.getValue();

            if (dtos.size() == 0) {
                // 该年月没有数据 不推送
                continue;
            }

            // 判断各个会计期间是否需要传递 只传递已经月结的数据
            // 暂时放行 测试用
            // 程哥改逻辑了 改成是这些业务表上传成功了 才去生成月结数据 所以这里不再校验是否月结
//            if (settledYearMonths.contains(yearMonth)) {

            List<VO> list = new ArrayList<>();
            for (DTO dto : dtos) {
                VO vo = converterFunc.apply(dto);
                list.add(vo);
            }

            sendListData(list, ListUtil.toSingleList(dtos, getIdFunc), batchId, accSet);

//            } else {
//                log.debug(dataApi.getDescription() + "该会计期间未月结，不进行对接：" + yearMonth);
//            }
        }
    }




    @Override
    protected String getCheckDuplicateGroupByFields() {
        return getMonthlyReportCheckDuplicateGroupByFields();
    }

    // 给资产负债表 债务公布表 债权公布表的查重字段
    protected String getMonthlyReportCheckDuplicateGroupByFields() {
        return "ZT_DATA_KEY, SSQJ";
    }

    @Override
    public void sendDeleteData(DuijieQuery duijieQuery, String batchId) throws JsonProcessingException {
        // 这类型不支持手动调用删除
    }

    @Override
    public List<Integer> getDataIds(ApiDataCollection apiDataCollection) {
        Map<String, List<DTO>> dataMap = getDataFunc().apply(apiDataCollection);
        List<Integer> result = new ArrayList<>();
        for (List<DTO> dataList : dataMap.values()) {
            result.addAll(ListUtil.toSingleList(dataList, getIdFunc()));
        }
        return result;
    }

    @Override
    public String getYearMonthColumnName() {
        return "KJQJ";
    }
}
